﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace CSC440TeamOne.MyHealth.Views
{
    public partial class MyHealthGoalConfig : Microsoft.Practices.CompositeWeb.Web.UI.Page, IMyHealthGoalConfigView
    {
        private MyHealthGoalConfigPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            repGoals.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(repGoals_ItemDataBound);

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            _presenter.OnSave();
        }

        void repGoals_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, HealthGoal>.Bind(e.Item, a =>
            {
                a.Find<CheckBox>("chkGoal").Text = a.BusinessItem.Description;

                a.Find<CheckBox>("chkGoal").Checked = a.BusinessItem.Selected;

                a.Find<CheckBox>("chkGoal").Attributes.Add("HealthGoalID", a.BusinessItem.ID.ToString());
            });
        }

        [CreateNew]
        public MyHealthGoalConfigPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


        public void Show(Common.MyHealthGoalConfigViewModel model)
        {
            IEnumerable<HealthGoal> goals = model.HealthGoals;

            if (goals.Any())
            {
                repGoals.DataSource = goals;

                repGoals.DataBind();
            }
        }


        public void Update(Common.MyHealthGoalConfigViewModel model)
        {
            foreach (RepeaterItem repeaterItem in repGoals.Items)
            {
                var chkGoal = repeaterItem.FindControl("chkGoal") as CheckBox;

                if (chkGoal != null)
                {
                    var healthGoalID = chkGoal.Attributes["HealthGoalID"];

                    var tempInt = 0;

                    if (!string.IsNullOrEmpty(healthGoalID) && int.TryParse(healthGoalID, out tempInt))
                    {
                        model.UpdateHealthGoal(tempInt, chkGoal.Checked);
                    }
                }
            }
        }


        public void OnSaveCompleted(Common.MyHealthGoalConfigViewModel model)
        {
            mvState.SetActiveView(vwCompleted);
        }
    }
}

